package com.outstanding.his.pretreat.dao.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.outstanding.his.pretreat.dao.IPretreatDao;
import com.outstanding.his.pretreat.domain.Predrug;
import com.outstanding.his.pretreat.domain.Pretreat;

public class HibernatePretreatDao extends HibernateDaoSupport implements
		IPretreatDao {

	@Transactional
	public Pretreat submitPretreat(Pretreat pretreat)
			throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(pretreat);
		return pretreat;
	}

	@Override
	public Pretreat getPretreat(Integer pretreatId) throws DataAccessException {
		Pretreat pretreat = (Pretreat) this.getSession().load(Pretreat.class,
				pretreatId);
		return pretreat;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pretreat getPretreatByPatientId(Integer patientId)
			throws DataAccessException {
		Pretreat pretreat = null;
		String sql = "FROM Pretreat pre WHERE pre.patient.patientId = :patientId";
		List<Object> list = this.getSession().createQuery(sql).setParameter("patientId", patientId).list();
		Iterator<Object> iterator = list.iterator();
		while(iterator.hasNext()) {
			pretreat = (Pretreat)iterator.next();
		}
		return pretreat;
	}

	@Override
	public Predrug createPredrug(Predrug predrug) throws DataAccessException {
		this.getSession().save(predrug);
		return predrug;
	}

	@Override
	public Predrug getPredrug(Integer predrugId) throws DataAccessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Collection<Predrug> getPredrugListByPatientId(Integer predrugId)
			throws DataAccessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	@Override
	public void deletePredrug(Predrug predrug) throws DataAccessException {
		this.getHibernateTemplate().delete(predrug);	
	}
	

}
